package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.PageParam;
import com.ruoyi.system.domain.SysOperLog;
import com.ruoyi.system.mapper.SysOperLogMapper;
import com.ruoyi.system.service.ISysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 操作日志 服务层处理
 *
 * @author ruoyi
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements ISysOperLogService {
    @Autowired
    private SysOperLogMapper operLogMapper;

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLog operLog) {
        operLogMapper.insertOperlog(operLog);
    }

    /**
     * 查询系统操作日志集合
     *
     * @param pageParam 分页参数
     * @param operLog   操作日志对象
     * @return 操作日志集合
     */
    @Override
    public Page<SysOperLog> selectOperLogList(PageParam pageParam, SysOperLog operLog) {
        if (pageParam == null) {
            pageParam = new PageParam(0, -1);
        }

        return this.query()
                .like(StrUtil.isNotEmpty(operLog.getOperIp()), "oper_ip", operLog.getOperIp())
                .like(StrUtil.isNotEmpty(operLog.getTitle()), "title", operLog.getTitle())
                .eq(operLog.getBusinessType() != null, "business_type", operLog.getBusinessType())
                .in(ArrayUtil.isNotEmpty(operLog.getBusinessTypes()), "business_type", ListUtil.toList(operLog.getBusinessTypes()))
                .eq(operLog.getStatus() != null, "status", operLog.getStatus())
                .like(StrUtil.isNotEmpty(operLog.getOperName()), "oper_name", operLog.getOperName())
                .ge(ObjectUtil.isNotEmpty(operLog.getParams().get("beginTime")), "oper_time", operLog.getParams().get("beginTime"))
                .le(ObjectUtil.isNotEmpty(operLog.getParams().get("endTime")), "oper_time", operLog.getParams().get("endTime"))
                .orderBy(StrUtil.isNotEmpty(pageParam.getOrderByColumn()), pageParam.asc(), StrUtil.toUnderlineCase(pageParam.getOrderByColumn()))
                .orderByDesc("oper_id")
                .page(new Page<>(pageParam.getPageNum(), pageParam.getPageSize()));
    }

    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(Long[] operIds) {
        return operLogMapper.deleteOperLogByIds(operIds);
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(Long operId) {
        return operLogMapper.selectOperLogById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        operLogMapper.cleanOperLog();
    }
}
